#!coding=utf-8
import random
import threading
import time

from  print_s import print_safe as print
# 创建一个包含三个线程的屏障
barrier = threading.Barrier(3)
# 初始化累加器
total = 0

# 线程函数
def worker(start, end):
    global total
    for i in range(start, end):
        total += i
    time.sleep(random.randint(1, 5))
    print(f"Worker {threading.current_thread().name} has added {end - start} numbers to the total.")
    barrier.wait()
    print(f"Worker {threading.current_thread().name} is proceeding after the barrier.")



# 创建并启动线程
threads = []
for i in range(3):
    start = i * 1000000
    end = (i + 1) * 1000000
    thread = threading.Thread(target=worker, args=(start, end))
    threads.append(thread)
    thread.start()

# 等待线程完成
for thread in threads:
    thread.join()

print(f"The final total is: {total}")
print(f"math calc total is: {sum(range(1, 3000000))}")
